﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using SymbolicLinkManageCore;
using SymbolicLinkManageCore.Exceptions;
using SymbolicLinkManageUI.Windows;

namespace SymbolicLinkManageUI.Lib
{
    public partial class SelectWindowView : ObservableObject
    {
        public static ObservableCollection<SelectListData> DesignData = new() 
        {
            new SelectListData(new() { LinkPointPath = AppDomain.CurrentDomain.BaseDirectory }) { IsSelect=true },
            new SelectListData(new() { linkType= MkLink.LinkType.HardLink, HardLinkGroup= [(HardLinkData)"E:\\worktemp\\FFmpegVideoClip.txt" , (HardLinkData)$"{AppDomain.CurrentDomain.BaseDirectory}.txt" ] })
        };

        private SelectWindow selectWindow;
        public SelectWindowView(SelectWindow windows, RecordData[] recordDatas) 
        {
            selectWindow = windows;
            recordDatas = recordDatas.GetRecordDataDistinct().ToArray();
            SelectListDatas = new(recordDatas.OrderBy(p=>p.LinkPointPath).Select(p => new SelectListData(p))); 
        }

        [ObservableProperty]
        private ObservableCollection<SelectListData> m_SelectListDatas = new();

        [RelayCommand]
        public void Submit()
        {
            selectWindow.selectLists = SelectListDatas.Where(p => p.IsSelect).Select(p => p.recordData).ToArray();
            selectWindow.DialogResult = true;
        }

        [RelayCommand]
        public void Cancel() => selectWindow.DialogResult = false;

        [RelayCommand]
        public void AllSelect() => SelectListDatas.ForEach(p => p.IsSelect = true);

        [RelayCommand]
        public void AllCancelSelect() => SelectListDatas.ForEach(p => p.IsSelect = false);

        [RelayCommand]
        public void ReverseSelection() => SelectListDatas.ForEach(p => p.IsSelect = !p.IsSelect);

        [RelayCommand]
        public void SetSelects(bool IsSelect) => selectWindow.ListBox.SelectedItems.Cast<SelectListData>().ForEach(p => p.IsSelect = IsSelect);

        [RelayCommand]
        public void ReverseSelections() => selectWindow.ListBox.SelectedItems.Cast<SelectListData>().ForEach(p => p.IsSelect = !p.IsSelect);
    }
}
